import { CircleShape } from "../../lib/kgame/d2/shape/CircleShape";
import { ECSCmp } from "../../lib/kgame/ecs/ECSCmp";
import { ColliderCmp } from "./ColliderCmp";

/**
 * 食物组件
 */
export class FoodCmp extends ECSCmp {

    /** 分数 */
    score: number;
    /** 碰撞器组件 */
    collider: ColliderCmp<CircleShape>;
    /** 剩余存活时间 -1 永久存活 */
    lifeTime: number;
    /** 十进制颜色 没有代表随机 */
    color: number

    /**
     * 获取对应分数的食物大小
     * @param
     */
    static getSize(score: number) {
        return 1 + score / 50 + Math.pow(score, 0.4) * 5;
    }

    /**
     * 初始化食物组件
     * @param score 分数
     * @param collider 碰撞器组件
     * @param lifeTime 剩余存活时间 -1 永久存活
     * @param color 十进制颜色 没有代表随机
     */
    onCreate(
        score: number,
        collider: ColliderCmp<CircleShape>,
        lifeTime = -1,
        color?: number
    ) {
        this.score = score;
        this.collider = collider;
        this.lifeTime = lifeTime;
        this.color = color;
        this.collider.shape.radius = FoodCmp.getSize(score) / 2;
        this.collider.shape.update(this.pos);
        return this;
    }

    protected onClear(): void {
        this.collider = null;
    }

}